# Maintainer: Christian Rebischke <chris.rebischke@archlinux.org>
# Maintainer: Maxim Baz <$pkgname at maximbaz dot com>
# Contributor: Daniel Martí <mvdan@mvdan.cc>

pkgname=shfmt
pkgver=3.7.0
pkgrel=1
pkgdesc="Format shell programs"
url="https://github.com/mvdan/sh"
license=('BSD')
arch=('loong64' 'x86_64')
depends=('glibc')
makedepends=('git' 'go' 'scdoc')
options=('!lto')
source=("${pkgname}-${pkgver}.tar.gz::https://github.com/mvdan/sh/archive/v${pkgver}.tar.gz")
b2sums=('5de1f5576f3e92f5cc892da730b62640b0922f879005e844b3e0b0d84a4e8f1a5f76e89c53152dd3de11c67da20411c5bddd44627ab31b66a72f950b1bf03393')

build() {
    cd "${srcdir}/sh-${pkgver}/cmd/shfmt/"

    export CGO_CPPFLAGS="${CPPFLAGS}"
    export CGO_CFLAGS="${CFLAGS}"
    export CGO_CXXFLAGS="${CXXFLAGS}"
    export CGO_LDFLAGS="${LDFLAGS}"
    export GOFLAGS="-buildmode=pie -trimpath -ldflags=-linkmode=external -mod=readonly -modcacherw"
    go build -ldflags "-linkmode=external -extldflags=$LDFLAGS -X main.version=${pkgver}"

    scdoc < "${pkgname}.1.scd" | gzip > "${pkgname}.1.gz"
}

check() {
    cd "${srcdir}/sh-${pkgver}/"
    go test ./...
}

package() {
    cd "${srcdir}/sh-${pkgver}/"
    install -Dm755 -t "${pkgdir}/usr/bin/" "cmd/${pkgname}/${pkgname}"
    install -Dm644 -t "${pkgdir}/usr/share/man/man1/" "cmd/${pkgname}/${pkgname}.1.gz"
    install -Dm644 -t "${pkgdir}/usr/share/licenses/${pkgname}/" LICENSE
}
